package cn.itcast;

import cn.itcast.jobs.HelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class HelloJobMain {
    public static void main(String[] args) {
        try {
            //1:创建调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
             //2:创建任务实例
            JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).
                    withIdentity("JobDetail_1").
                    build();

            //设置参数，在定时任务执行时可以动态获取这些参数
            jobDetail.getJobDataMap().put("key1","value1");
            jobDetail.getJobDataMap().put("key2","value2");

            //构建cron表达式
            CronScheduleBuilder cronScheduleBuilder =
                    CronScheduleBuilder.cronSchedule("1,5,11,20,27,35 * * * * ?");
            //3:创建触发器Trigger
            Trigger trigger = TriggerBuilder.newTrigger().
                    withIdentity("Trigger_1").
//                    withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).
                    withSchedule(cronScheduleBuilder).
                    build();

            //4:触发器和任务关联
            scheduler.scheduleJob(jobDetail,trigger);

            //5:启动任务调度器
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}